<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Models\Position
 *
 * @property int $id
 * @property string|null $title 标题
 * @property string|null $phone 手机
 * @property string|null $position 职位
 * @property string|null $type 职位类型
 * @property string|null $money_min 最低工资
 * @property string|null $money_max 最高工资
 * @property string|null $people 招聘人数
 * @property string|null $work_time 工作时间
 * @property string|null $gender 要求性别
 * @property string|null $edu 要求教育
 * @property string|null $contacts 联系人
 * @property string|null $company 公司
 * @property string|null $address 参考地址
 * @property string|null $content 详细内容
 * @property string|null $posted_at 发布时间
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null $holiday 假期指标
 * @property string|null $deleted_at
 * @property string|null $geo geo
 * @property string|null $lat 纬度
 * @property string|null $lng 经度
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\BlackList[] $black_lists
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Favorite[] $favorites
 * @property-read mixed $money
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\History[] $histories
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Tagged[] $tags
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Position onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereCompany($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereContacts($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereEdu($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereGender($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereGeo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereHoliday($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereLat($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereLng($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereMoneyMax($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereMoneyMin($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position wherePeople($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position wherePosition($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position wherePostedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Position whereWorkTime($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Position withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Position withoutTrashed()
 * @mixin \Eloquent
 */
class Position extends Model
{
    use SoftDeletes;
    protected $appends = ['money'];

    public static function boot()
    {
        parent::boot();
        self::addGlobalScope('default_order', function ($query) {
            /** @var Position $query */
            $query->orderBy('posted_at','desc');
        });
    }

    public function histories()
    {
        return $this->hasMany(History::class);
    }

    public function black_lists()
    {
        return $this->hasMany(BlackList::class);
    }

    public function favorites()
    {
        return $this->hasMany(Favorite::class);
    }

    public function getMoneyAttribute()
    {
        if ($this->money_max == $this->money_min) {
            if ($this->money_min == 0) {
                return "面议";
            }
            return $this->money_min;
        }
        return $this->money_min . '-' . $this->money_max;
    }

    public function tags()
    {
        return $this->morphMany(Tagged::class, 'module');
    }
}
